//
//  mailHMViewController.m
//  ScrollViews
//
//  Created by QQImac on 2018/3/30.
//  Copyright © 2018年 Mr.Lee. All rights reserved.
//

#import "QQRegistersViewController.h"
#import "UILabel+YBAttributeTextTapAction.h"
#import "QQAgreementController.h"
#import "MainTabbarController.h"



@interface QQRegistersViewController ()
@property (weak, nonatomic) IBOutlet UIButton *agreeBtn;

@property (weak, nonatomic) IBOutlet UITextField *numberTextF;
@property (weak, nonatomic) IBOutlet UITextField *codeTextF;
@property (weak, nonatomic) IBOutlet UITextField *passWorldTextF;
@property (weak, nonatomic) IBOutlet UITextField *passWorldsTextf;
@property (weak, nonatomic) IBOutlet UITextField *inviteNumber;
@property (weak, nonatomic) IBOutlet UIButton *yzmBtn;

@property (weak, nonatomic) IBOutlet UIButton *forgetBtn;
@property (weak, nonatomic) IBOutlet UILabel *agreeLable;

@end

@implementation QQRegistersViewController
{
    NSTimer *timer;
    NSInteger iSmsValidateSeconds;
    NSInteger iSmsValidateSecondsDefault;
    NSDictionary *_userDict;
}
- (void)viewDidLoad {
    self.title = @"注册";
    
    iSmsValidateSecondsDefault=60;
    [super viewDidLoad];
    [self initWithSubView];
    // 添加监听
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:)
                                                name:@"UITextFieldTextDidChangeNotification"
                                              object:_numberTextF];
}

-(void)initWithSubView{
    
    
    [_passWorldTextF setSecureTextEntry:YES];
    [_passWorldsTextf setSecureTextEntry:YES];
    
    WS(weakSelf);
    [_agreeLable yb_addAttributeTapActionWithStrings:@[@"《用户协议》"] tapClicked:^(NSString *string, NSRange range, NSInteger index) {
        QQAgreementController* vc = [[QQAgreementController alloc]init];
        [weakSelf.navigationController pushViewController:vc animated:YES];
    }];
    _agreeLable.enabledTapEffect = NO;
}


- (IBAction)isAgreeClick:(UIButton *)sender {
    sender.selected = !sender.selected;
}


- (IBAction)btnClick:(UIButton *)btn {
    if  ((_numberTextF.text.length==0)||(_passWorldTextF.text.length==0)||(_codeTextF.text.length == 0)||(_passWorldsTextf.text.length == 0)){
        [[XNProgressHUD shared] showErrorWithTitle:@"请输入完整信息"];
        return;
    }
    
    if (!self.agreeBtn.selected) {
        [[XNProgressHUD shared] showErrorWithTitle:@"请遵守协议"];
        return;
    }
    if (!(_numberTextF.text.length == 11)) {
        [[XNProgressHUD shared] showErrorWithTitle:@"请注意手机号格式"];
        return;
    }
    
    if(_codeTextF.text.length !=6){
        [[XNProgressHUD shared] showErrorWithTitle:@"请注意验证码格式"];
        return;
    }
    if(_passWorldTextF.text.length <6||_passWorldTextF.text.length>16){
        [[XNProgressHUD shared] showErrorWithTitle:@"请注意密码格式"];
        return;
    }
    if(![_passWorldTextF.text isEqualToString:_passWorldsTextf.text]){
        [[XNProgressHUD shared] showErrorWithTitle:@"2次密码不一样哦"];
        
        return;
    }
    
    if (_inviteNumber.text.length == 0) {
        
        LEEAlert.alert.config.LeeContent(@"邀请码不能为空，请联系客服").LeeAddAction(^(LEEAction *action) {
            action.backgroundColor = kColorWithRGB(60, 70, 118);
            action.titleColor = [UIColor whiteColor];
            action.title = @"确认";
            action.font = [UIFont systemFontOfSize:17];
            action.clickBlock = ^{
                [self callPhone];
            };
        }).LeeShow();
        
        return;
    }
    
    NSDictionary*dic = @{ @"phoneNum":_numberTextF.text,
                          @"password":_passWorldTextF.text,
                          @"inviteCode": _inviteNumber.text,
                          @"repeatPwd":_passWorldsTextf.text,
                          @"message":_codeTextF.text,
                          @"source":@"1"
                          };
    NSMutableDictionary *varDict = [[NSMutableDictionary alloc] initWithDictionary:dic];
    if ([_userDict count] != 0 && _userDict != nil) {
        [varDict  addEntriesFromDictionary:_userDict];
    }
    
    [[XNProgressHUD shared] showLoadingWithTitle:@" 注册中，请稍候 " maskType:(XNProgressHUDMaskTypeBlack)];
    [[LMJRequestManager sharedManager] POST:KBaseURL(@"user/regist") parameters:varDict completion:^(LMJBaseResponse *response) {
        [[XNProgressHUD shared] dismiss];
        if (response.responseModel.status == 1) {
            NSDictionary* dict = response.responseModel.data;
            QQUserInfo *userInfo = [QQUserInfo mj_objectWithKeyValues:dict];
            userInfo.user_Number = _numberTextF.text;
            userInfo.user_PassW = @"";
            [userInfo saveUserAccount];
            [[XNProgressHUD shared] showSuccessWithTitle:@"注册成功，自动登录"];
            
            MainTabbarController *mainTabBarController = [[MainTabbarController alloc] init];
            self.view.window.rootViewController = mainTabBarController;
        } else {

            [[XNProgressHUD shared] showErrorWithTitle:response.errorMsg];
        }
    }];
    
}
- (IBAction)callService:(id)sender {
    
    [LEEAlert alert].config.LeeAddContent(^(UILabel *label) {
        label.text = @"4008-055575";
        label.textColor = kColorWithRGB(60, 70, 118);
        label.font = [UIFont systemFontOfSize:17];
    }).LeeAddAction(^(LEEAction *action) {
        [action setTitle:@"取消"];//60    70    118
        [action setTitleColor:kColorWithRGB(60, 70, 118)];
    }).LeeAddAction(^(LEEAction *action) {
        [action setTitle:@"呼叫"];
        [action setTitleColor:[UIColor whiteColor]];
        action.backgroundColor = kColorWithRGB(60, 70, 118);
        action.clickBlock = ^{
            [self callPhone];
        };
    }).LeeShadowOpacity(3).LeeCornerRadius(5).LeeShadowOpacity(0.4).LeeShow();
    
}

- (void)callPhone {
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"4008-055575"];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}

- (IBAction)getYZM:(id)sender {
    if (_numberTextF.text.length <11&&_numberTextF.text.length!=0) {
        [[XNProgressHUD shared] showErrorWithTitle:@"手机号码输入有误"];
        return;
    }
    
    NSDictionary *parameters = @{@"phoneNum": _numberTextF.text,
                                 @"type": @1};
    
    [[LMJRequestManager sharedManager] POST:KBaseURL(@"user/sendmsg/free") parameters:parameters completion:^(LMJBaseResponse *response) {
        if (response.responseModel.status == 1) {
            [[XNProgressHUD shared] showSuccessWithTitle:response.responseModel.message];
            [Utility startTime:sender];
        }
    }];
}


-(void)textFiledEditChanged:(NSNotification *)obj{
    UITextField *textField = (UITextField *)obj.object;
    
    NSString *toBeString = textField.text;
    NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textField markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length > 11) {
                textField.text = [toBeString substringToIndex:11];
            }
        }
        // 有高亮选择的字符串，则暂不对文字进行统计和限制
        else{
            
        }
    }
    // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
    else{
        if (toBeString.length > 11) {
            textField.text = [toBeString substringToIndex:11];
        }
    }
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self
                                                   name:@"UITextFieldTextDidChangeNotification"
                                                 object:_numberTextF];
}
@end
